import { defineStore } from 'pinia'
import { ref } from 'vue'
import type { User } from '../../types/user'
import { removeStorage, saveStorage } from '../../utils/storege'

export const useUserStore = defineStore('cp-user', () => {
    const user = ref<User>({} as User)

    // 存储用户信息（登录）
    const setUser = (userData: User) => {
        user.value = userData
        saveStorage('users', userData)
    }

    // 删除用户信息（退出）
    const delUser = () => {
        user.value = {} as User
        removeStorage('users')
    }

    // 返回变量和方法
    return {
        user,
        setUser,
        delUser
    }
})